<?php

class DataAction extends Action 
{
	public function _initialize()
	{
		$this->assign('jumpUrl',__URL__);
 		if(!isset($_SESSION['adminid']))
 		{ 
 			 $url=__APP__.'/Index/login';
 			 redirect($url);return;
 		}		
	}    
    public function index()
    {
		$Data=D("Data");
		$count=$Data->count();
		import("ORG.Util.Page");
		$listRows=10;
		$p=new page($count,$listRows);
		$list=$Data->findAll(null,'*','orderid desc,addtime desc',$p->firstRow.','.$p->listRows);
		$page=$p->show();
		if ($list!==false) {
			$this->assign('page',$page);
			$this->assign('list',$list);			
		}
    				
		$this->display();
    }
    public function add()
    {
    	if(!$this->isPost())
    	{
    		$this->display();
    	}
    	else
    	{
	        $title=isset($_POST['title'])?trim($_POST['title']):'';
	        $intro=isset($_POST['intro'])?trim($_POST['intro']):'';
	        $catid=isset($_POST['catid'])?(int)($_POST['catid']):0;
	        $orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):0;
	        $htmlbody=isset($_POST['htmlbody'])?trim($_POST['htmlbody']):'';	  
	        $recomm=isset($_POST['recomm'])?(int)($_POST['recomm']):0;      
            
    		$Data=D("Data");
            if($orderid==0)
            {	
            	$orderid=$Data->max('orderid');
            	$orderid=$orderid+1;
            }                
            $data=array(
            	'title'=>$title,
            	'intro'=>$intro,
            	'catid'=>$catid,
            	'orderid'=>$orderid,
            	'recomm'=>$recomm,
            	'htmlbody'=>$htmlbody,
            );	    				
    		if(!$Data->create($data))
    		{
    			$this->error($Data->getError());return;
    		}
    		if(!$Data->add())
    		{ 		
    			$this->error(L('_INSERT_FAIL_'));return;
    		}
    		$condition['catid']=$catid;
    		D("Datacat")->setInc('count',$condition);
    		
    		$this->cache();
    		$this->assign('jumpUrl',__URL__);
    		$msg="技术资料添加成功";
    		$this->success($msg);
    	}
    }
    public function edit()
    {
    	if(!$this->isPost())
    	{
			$id=isset($_GET['id'])?(int)($_GET['id']):0;
			if (!$id){
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}
			$Data=D("Data");
			$temp=$Data->find($id);
			if (!$temp){
					$this->error(L('_SELECT_NOT_EXIST_'));return;
			}			    					
			$this->assign('vo',$temp);
			$this->display();			    			
    	}
    	else
    	{
			$id=isset($_POST['id'])?(int)($_POST['id']):0;
			$temp=D('Data')->find($id);
			if (!$id||!$temp)
			{ 
				$this->error(L('_SELECT_NOT_EXIST_'));return;
			}			

	        $title=isset($_POST['title'])?trim($_POST['title']):'';
	        $url=isset($_POST['url'])?trim($_POST['url']):'';
	        $intro=isset($_POST['intro'])?trim($_POST['intro']):'';
	        $catid=isset($_POST['catid'])?(int)($_POST['catid']):0;
	        $orderid=isset($_POST['orderid'])?(int)($_POST['orderid']):0;
	        $htmlbody=isset($_POST['htmlbody'])?trim($_POST['htmlbody']):'';
	        $recomm=isset($_POST['recomm'])?(int)($_POST['recomm']):0;
	        
            $data=array(
            	'id'=>$id,
				'title'=>$title,
				'url'=>$url,
            	'intro'=>$intro,
            	'catid'=>$catid,
            	'orderid'=>$orderid,
            	'recomm'=>$recomm,
            	'htmlbody'=>$htmlbody,
            );	 	        	        			
			
			$Data=D("Data");
			if(!$Data->create($data))
			{
				$this->error($Data->getError());return;
			}
			if(!$Data->save())
			{
				$this->error(L('_UPDATE_FAIL_'));return;
			}	
			$this->cache();		
			$this->assign('jumpUrl',__URL__);
			$msg="技术资料修改成功";
			$this->success($msg);
    	}    	
    }
	public function del()
	{
		$id=isset($_GET['id'])?(int)($_GET['id']):0;
		if (!$id){ $this->error(L('_SELECT_NOT_EXIST_'));return;}
		$Data=D("Data");
		$temp=$Data->find($id);
		if (!$temp){ $this->error(L('_SELECT_NOT_EXIST_'));return;}		
		if(!$Data->deleteById($id)){
			$this->error(L('_DELETE_FAIL_'));return;
		}
    	$condition['catid']=$temp['catid'];
    	D("Datacat")->setDec('count',$condition);
    			
		$this->cache();
		$this->redirect();
	}	
	private function cache()
	{
		$Data=D("Data");
		$condition['recomm']=1;
		$list=$Data->findAll($condition,'id,title,url,addtime','orderid desc,addtime desc',10);
		$filename=C('MYCACHE_PATH').'/data.php';
		if(@file_exists($filename)){ @unlink($filename);}
    	$content="<?php\n \$data=".var_export($list,true)."\n?>";
    	@!file_put_contents($filename,$content);	
	}			
}
?>